package com.cdivtc.management.service;

import com.cdivtc.management.controller.StudentsDoTasksController;
import com.cdivtc.management.dao.*;
import com.cdivtc.management.pojo.*;
import com.cdivtc.management.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class StudentsDoTasksService {
    @Autowired
    private StudentsDoTasksMapper studentsDoTasksMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private FilesurlMapper filesurlMapper;

    @Autowired
    private StuTaskStatusMapper stuTaskStatusMapper;
    @Autowired
    private TeacherReleaseTaskMapper teacherReleaseTaskMapper;
    @Autowired
    private TopicTitleMapper topicTitleMapper;
    @Autowired
    private StudentService studentService;

    public StudentsDoTasks addStuTask(StudentsDoTasksController.AddBean addBean) {
        StudentsDoTasks st = new StudentsDoTasks();
        String id = idWorker.nextId() + "";
        st.setId(id);
        st.setState("0");
        st.setStuId(addBean.getStuId());
        st.setTaskId(addBean.getTaskId());
        st.setTeaId(addBean.getTeaId());
        st.setSubmitDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        st.setStuRemarks(addBean.getStuRemarks());
        int insert = studentsDoTasksMapper.insert(st);

        Example e = new Example(StuTaskStatus.class);
        Example.Criteria c = e.createCriteria();
        c.andEqualTo("taskId", st.getTaskId());
        c.andEqualTo("stuId", st.getStuId());
//        List<StuTaskStatus> stuTaskStatuses = stuTaskStatusMapper.selectByExample(e);
        StuTaskStatus stuTaskStatus = stuTaskStatusMapper.selectOneByExample(e);
        if (stuTaskStatus == null) {
            StuTaskStatus sts = new StuTaskStatus();
            String sid = idWorker.nextId() + "";
            sts.setId(sid);
            sts.setStatus("1");
            sts.setStuId(st.getStuId());
            sts.setTaskId(st.getTaskId());
            stuTaskStatusMapper.insert(sts);
        } else {
            stuTaskStatus.setStatus("1");
            stuTaskStatusMapper.updateByExample(stuTaskStatus,e);
        }

        if (insert > 0) {
            for (String s : addBean.getStuUpFile()) {
                Filesurl filesurl = filesurlMapper.selectByPrimaryKey(s);
                filesurl.setTaskId(id);
                filesurlMapper.updateByPrimaryKey(filesurl);
            }
            return st;
        }
        return null;
    }

    public List<Map<String, Object>> selStuTask(String stuId, String taskId) {
        Example e = new Example(StudentsDoTasks.class);
        Example.Criteria c = e.createCriteria();
        c.andEqualTo("taskId", taskId);
        c.andEqualTo("stuId", stuId);
        List<StudentsDoTasks> studentsDoTasks = studentsDoTasksMapper.selectByExample(e);
        List<Map<String, Object>> list = new ArrayList<>();
        for (StudentsDoTasks studentsDoTask : studentsDoTasks) {
            Map<String, Object> map = new HashMap<String, Object>();
            String taskId1 = studentsDoTask.getTaskId();
            TeacherReleaseTask teacherReleaseTask = teacherReleaseTaskMapper.selectByPrimaryKey(taskId1);
            map.put("studentsDoTask", studentsDoTask);
            map.put("teacherReleaseTask", teacherReleaseTask);
            list.add(map);
        }
        return list;
    }

    public StudentsDoTasks selStuTaskById(String id) {
        StudentsDoTasks studentsDoTasks = studentsDoTasksMapper.selectByPrimaryKey(id);
        if (studentsDoTasks != null) {
            return studentsDoTasks;
        }
        return null;
    }

    public List<Map<String, Object>> selStuTaskByTeaId(String teaId) {
        Example e = new Example(StudentsDoTasks.class);
        Example.Criteria c = e.createCriteria();
        c.andEqualTo("teaId", teaId);
        c.andEqualTo("state", "0");
        List<StudentsDoTasks> studentsDoTasks = studentsDoTasksMapper.selectByExample(e);
        List<Map<String, Object>> list = new ArrayList<>();
        for (StudentsDoTasks studentsDoTask : studentsDoTasks) {
            Map<String, Object> map = new HashMap<>();
            TeacherReleaseTask teacherReleaseTask = teacherReleaseTaskMapper.selectByPrimaryKey(studentsDoTask.getTaskId());
            String topicId = teacherReleaseTask.getTopicId();
            TopicTitle topicTitle = topicTitleMapper.selectByPrimaryKey(topicId);
            Map<String, Object> map1 = studentService.stuInfoById(studentsDoTask.getStuId());

            map.put("topicTitle", topicTitle);
            map.put("studentsDoTask", studentsDoTask);
            map.put("stuInfo", map1);
            map.put("teacherReleaseTask", teacherReleaseTask);
            list.add(map);
        }
        return list;
    }

    public StudentsDoTasks updStuTask(String id, String teaDesc, String flag, String taskId, String stuId) {
        StudentsDoTasks studentsDoTasks = studentsDoTasksMapper.selectByPrimaryKey(id);
        studentsDoTasks.setState(flag);
        studentsDoTasks.setTeaRemarks(teaDesc);
        int i = studentsDoTasksMapper.updateByPrimaryKey(studentsDoTasks);

        Example e = new Example(StuTaskStatus.class);
        Example.Criteria c = e.createCriteria();
        c.andEqualTo("taskId", taskId);
        c.andEqualTo("stuId", stuId);
        StuTaskStatus stuTaskStatus = stuTaskStatusMapper.selectOneByExample(e);
        if(flag.equals("1")){
            stuTaskStatus.setStatus("3");
        }
        if(flag.equals("2")){
            stuTaskStatus.setStatus("2");
        }
        int i1 = stuTaskStatusMapper.updateByExample(stuTaskStatus,e);
        System.out.println("StudentsDoTasksService line 141= " + i1);


        if (i > 0) {
            return studentsDoTasks;
        }
        return null;
    }
}
